<%
Dim CDEmploymentLength
Dim sTempErrorMsg
Dim sSaveMessage

lstRFD					= Trim(Request.Form("lstRFD"))
lstRFDIndex				= Trim(Request.Form("txtlstRFD"))
nEmployeeNo				= Trim(Request.Form("txtEmployeeNo"))
dtCDEmployment			= Trim(Request.Form("txtCDEmployment"))
nTaxYear				= Trim(Request.Form("txtTaxYear"))
lstYN					= Trim(Request.Form("lstYN"))
lstYNIndex				= Trim(Request.Form("txtlstYN"))
nTaxPayersAnnualIncome	= Trim(Request.Form("txtTaxPayersAnnualIncome"))
nGrossInc				= Trim(Request.Form("txtGrossInc"))
nEstAdmExp				= Trim(Request.Form("txtEstAdmExp"))
nTaxPayable				= Trim(Request.Form("txtTaxPayable"))
nPerTaxDed				= Trim(Request.Form("txtPerTaxDed"))


sErrorMessage			= ValidateFormFields()



if sErrorMessage = "" then
	sSaveMessage = SaveRequest()
end if

if instr(UCASE(sSaveMessage), "SUCCESS") and sErrorMessage = "" then
	Response.redirect "../directive/directive.asp"	
End If


'------------------------------------------------------ Validation on fields -----------------------------------------------

Private Function ValidateFormFields
	Dim sValidateMessage
	Dim aFocusFields()
	Dim strDecimal

	' Intialize
	sValidateMessage = ""
	Redim aFocusFields(7)
				
	'Selected Index -  Reason For Directive
	If lstRFD = "" then
		sValidateMessage = sValidateMessage & "Please specify a Reason for the Directive" & "<BR>"
		aFocusFields(1) = "lstRFD"
	end if
	
	' Check CDEmployment
	If dtCDEmployment <> "" then
		sTempErrorMsg = ValidateEmpDate(dtCDEmployment)
		If Len(sTempErrorMsg) > 0 Then
			sValidateMessage = sValidateMessage & sTempErrorMsg & "<BR>"
			aFocusFields(2) = "txtCDEmployment"
		End If
	End If
	
	' Check TaxYear
	If nTaxYear = "" then
		sValidateMessage = sValidateMessage & "Please specify the Tax Year" & "<BR>"
		aFocusFields(3) = "txtTaxYear"
	Else
		sTempErrorMsg = TaxYear(nTaxYear)
		If Len(sTempErrorMsg) > 0 Then
			sValidateMessage = sValidateMessage & sTempErrorMsg & "<BR>"
			aFocusFields(3) = "txtTaxYear"
		End If
		
	End If
	
	If Len(nTaxYear) < 4 Then
		sValidateMessage = sValidateMessage & "Please specify the tax year as YYYY" & "<BR>"
		aFocusFields(3) = "txtTaxYear"
	End If 
	
	' Check Selected Index on Same Request Directive 
	If lstYN = "" Then
		sValidateMessage = sValidateMessage & "Please specify whether a Request for the same Directive type was submitted by the current employer for the previous year" & "<BR>"
		aFocusFields(4) = "txtlstYN"
	End If
	
	' Check Annual Income
	If nTaxPayersAnnualIncome = "" Then
		sValidateMessage = sValidateMessage & "Please specify the Annual Income" & "<BR>"
		aFocusFields(5) = "txtTaxPayersAnnualIncome"
	ElseIf IsNumeric(nTaxPayersAnnualIncome) = False Then
		sValidateMessage = sValidateMessage & "Enter a valid numeric amount for Annual Income" & "<br>"
		aFocusFields(5) = "txtTaxPayersAnnualIncome"
	End If
	
	' Check Gross Income
	If nGrossInc = "" Then
		sValidateMessage = sValidateMessage & "Please specify the Gross Income" & "<BR>"
		aFocusFields(6) = "txtGrossInc"
	ElseIf nGrossInc <> "" Then
		If InStr(nGrossInc, ".") <> 0 Then
		strDecimal = Right(nGrossInc, 3)
			If InStr(1, strDecimal, ".") <> 1 Then
			sValidateMessage = sValidateMessage & "Please capture the Estimated Gross Income in the format 00 or 00.00" & "<br><br>"
			End If
		End If
	ElseIf IsNumeric(nGrossInc) = False Then
		sValidateMessage = sValidateMessage & "Enter a valid numeric amount for Gross Income" & "<br>"
		aFocusFields(6) = "txtGrossInc"
	End If
	
	'Admissible Expenditure
	If nEstAdmExp <> "" Then
		If InStr(nEstAdmExp, ".") <> 0 Then
		strDecimal = Right(nEstAdmExp, 3)
			If InStr(1, strDecimal, ".") <> 1 Then
			sValidateMessage = sValidateMessage & "Please capture the Estimated Admissable Expenditure in the format 00 or 00.00" & "<br><br>"
			End If
		End If
	End If
	
	'Tax Payable
	If nTaxPayable <> "" Then
		If InStr(nTaxPayable, ".") <> 0 Then
		strDecimal = Right(nTaxPayable, 3)
			If InStr(1, strDecimal, ".") <> 1 Then
			sValidateMessage = sValidateMessage & "Please capture the Tax Payable in the format 00 or 00.00" & "<br><br>"
			End If
		End If
	End If
	
	' Check Request Percentage dDeduction
	If nPerTaxDed = "" Then
		sValidateMessage = sValidateMessage & "Please specify the Percentage of Tax Deduction" & "<BR>"
		aFocusFields(7) = "txtPerTaxDed"
	ElseIf IsNumeric(nPerTaxDed) = False Then
		sValidateMessage = sValidateMessage & "Enter a valid numeric amount for Percentage Tax Deduction" & "<br>"	
		aFocusFields(7) = "txtPerTaxDed"
	Else
		If Len(nPerTaxDed) > 2 Then
			sValidateMessage = sValidateMessage & "Please enter a 2 digit percentage value. " & "<BR>"
			aFocusFields(7) = "txtPerTaxDed"
		End If
	End If
	
	If isNumeric(nPerTaxDed) and nPerTaxDed <> "" then 
		if cdbl(nPerTaxDed) = 0 Then
			sValidateMessage = sValidateMessage & "The Percentage of Tax Deduction cannot be zero" & "<BR>"
			aFocusFields(7) = "txtPerTaxDed"
		end if
	end if
	
	sFocusSet = GetFocusField(aFocusFields)
	
	'Return the error message if any
	ValidateFormFields = sValidateMessage
						
End Function '  ValidateFormFields

'-------------------------------------------------------------------------------------------------------------------------






'----------------------------------------- Validate Commencement Employment Date -----------------------------------------
Public Function ValidateEmpDate(ByVal dtCDEmployment)

Dim sInputValue
Dim sReturnValue
Dim sSubmitted
Dim sErrorMessage
Dim dtStringDateYear
Dim dtStringMonth
Dim dtStringday


sErrorMessage = ""

If Len(dtCDEmployment) < 8 Then
    sErrorMessage = "Please enter the commencement date in the format YYYYMMDD."
else
	dtStringDateYear = Left(dtCDEmployment, 4)
	dtStringMonth = Mid(dtCDEmployment,5,2)
	dtStringday = Right(dtCDEmployment,2)

	If  dtCDEmployment <= dtBirthDate Then
		sErrorMessage = "The commencement date of employment precedes the birthdate of the taxpayer."	
	End If

	If Not (dtStringDateYear >= "19")  Then
	    sErrorMessage = "Please enter the commencement date in the format YYYYMMDD."
	ElseIf dtStringMonth > 12 Then
		sErrorMessage = "Please enter the commencement date in the format YYYYMMDD."
	ElseIf dtStringday > 31 Then
		sErrorMessage = "Please enter the commencement date in the format YYYYMMDD."
	End If
end if

ValidateEmpDate = sErrorMessage

End Function 'ValidateEmpDate

'--------------------------------------------------------------------------------------------------------------------------





'-------------------------------------------------- Validate Tax Year -----------------------------------------------------
Public Function TaxYear(nTaxYear)
Dim dtCurrentDate
Dim dtCurrentYear
Dim dtNextYear
Dim dtCurrentMonth
Dim dtTaxYear
Dim dtNow

dtNow = now

dtCurrentMonth = Month(dtNow)'9
dtCurrentYear = Year(dtNow)'2001

	If cdbl(dtCurrentMonth) = 1 OR cdbl(dtCurrentMonth) = 1 Then 'if the month is January or February, the tax year is the current year.
		dtTaxYear = cdbl(dtCurrentYear)
	Else
		dtTaxYear = cdbl(dtCurrentYear + 1) '2002
	End If

	If cdbl(nTaxYear) > cdbl(dtTaxYear) Then
		TaxYear = "" & nTaxYear & " is not a valid tax year. The tax year can only be " & dtTaxYear & " or earlier."
	End If

End Function 'TaxYear
'--------------------------------------------------------------------------------------------------------------------------




'------------------------------------------------------ Save IRP3B request details ----------------------------------------

Public Function SaveRequest()

	Dim FormIRP3bSave
	Dim SaveFormIRP3b
	Dim dllDateConvert

	If Len(sErrorMessage) = 0 Then
		
		Set dllDateConvert = Server.CreateObject("KeyTax.clsDateConvert")
		
		if dtBirthDate <> "" and len(dtBirthDate) = 8 then
			dtBirthDate = dllDateConvert.StringDateToLongDate(dtBirthDate)
		End if
		
		if dtCDEmployment <> "" and len(dtCDEmployment) = 8 then
			dtCDEmployment = dllDateConvert.StringDateToLongDate(dtCDEmployment)
		End if

		
		set FormIRP3bSave = Server.CreateObject("KeyTax.clsLoadIRP3b")
			SaveFormIRP3b = FormIRP3bSave.SaveFormIRP3b(session("KeyTaxUser"), sNoTaxRefNoRsn, sNoTaxRefNoText, nIncomeTaxReferenceNo, nRSAID, sOtherID, sSurname, sInitials, sFirstNames, dtBirthDate, sPost1, sPost2, sPost3, sPost4, sPostCode, sHome1, sHome2, sHome3, sHome4, sHomeCode, nTaxYear, lstRFD, nTaxPayersAnnualIncome, lstYN, nGrossInc, nPerTaxDed, dtCDEmployment, nEstAdmExp, nTaxPayable, sEmailTo, nEmployeeNo, nEmpID)
			session("SaveMessage") = SaveFormIRP3b
		
	End If

	Set FormIRP3bSave = nothing
	
	SaveRequest = SaveFormIRP3b
	
End Function

'----------------------------------------------------------------------------------------------------------------------------

	
	
%>